<?php
class response
{
	public $url='';
	public $http_code=0;
	public $exists=false;
	
	public function __construct()
	{
		$this->url='';
		$this->http_code=0;
		$this->exists=false;
	}

	/**
	* Verifies that a url exists truly.
	* It checks the page by opening it and reading the response code.
	*/
	public function url_exists($url='')
	{
		# Articles used to create this body part:
		#   http://www.wrichards.com/blog/2009/05/php-check-if-a-url-exists-with-curl/
		#   http://css-tricks.com/snippets/php/check-if-website-is-available/
		
		if(!filter_var($url, FILTER_VALIDATE_URL))
		{
			#echo "Invalid URL: {$url}";
			return false;
		}
	
		$handle = curl_init();
		curl_setopt($handle, CURLOPT_URL, $url);
		curl_setopt($handle, CURLOPT_HEADER, true);
		curl_setopt($handle, CURLOPT_NOBODY, true);
		curl_setopt($handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); # We have the value already
		curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, defined('TIMEOUT')?constant('TIMEOUT'):5);
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($handle, CURLOPT_VERBOSE, false);
		curl_setopt($handle, CURLOPT_SSLVERSION, 3);
		curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
		$response = curl_exec($handle);
		$http_code = (int)curl_getinfo($handle, CURLINFO_HTTP_CODE);
		$url_exists = ($http_code>=200 && $http_code<400);
		curl_close($handle);

		# Update the information
		$this->url=$url;
		$this->http_code = $http_code;
		$this->exists=$url_exists;

		return($url_exists);
	}
}
?>